package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.AccessTokenRequest;
import sz.szsmk.citizencard.model.network.bean.request.AddUserCardFavReq;
import sz.szsmk.citizencard.model.network.bean.request.QueryUserCardRequest;
import sz.szsmk.citizencard.model.network.bean.response.GetUserBindCardRes;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardResponse;
import sz.szsmk.citizencard.mvp.contract.IUserAddCardContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/16 10:47
 * description:
 */
public class UserAddCardPresenter extends IBasePresenter
        implements IUserAddCardContact.IUserAddCardPresenter<IUserAddCardContact.IUserAddCardView> {


    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public UserAddCardPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void queryUserCard() {
        QueryUserCardRequest cardRequest = new QueryUserCardRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        cardRequest.setAccess_token(access_token);
        cardRequest.setType("01");


        mNetworkDateSource.queryUserCard(cardRequest, new SubscribeResult<QueryUserCardResponse>() {
            @Override
            protected void onOk(QueryUserCardResponse response) {
                List<QueryUserCardResponse.DataBean> data = response.getData();
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "queryUserCard====" + json);
                if (data != null) {
                    ((IUserAddCardContact.IUserAddCardView) mView).queryUserCardSucc(data);
                }
            }

            @Override
            protected void onNext(QueryUserCardResponse response) {
                super.onNext(response);
                String code = response.getCode();
            }

            @Override
            protected void onFailure(QueryUserCardResponse response) {
                super.onFailure(response);
                String code = response.getCode();
                ((IUserAddCardContact.IUserAddCardView) mView).queryUserCardFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                String message = throwable.getMessage();
            }
        });

    }


    @Override
    public void addUserCardFav(QueryUserCardResponse.DataBean dataBean) {
        AddUserCardFavReq cardFavReq = new AddUserCardFavReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        cardFavReq.setACardNo(dataBean.getACardNo());
        cardFavReq.setAccess_token(access_token);
        cardFavReq.setBalanceMoney(dataBean.getCardBalance());
        if (dataBean.getCardNo() != null) {
            cardFavReq.setCardNo(dataBean.getCardNo());
        } else {
            cardFavReq.setCardNo("");
        }
        cardFavReq.setCardState(dataBean.getCardState());
        cardFavReq.setEndTime(dataBean.getEndTime());
        cardFavReq.setFuncCode("01");
        cardFavReq.setFuncName(dataBean.getFuncName() + "");
        cardFavReq.setPackageName(dataBean.getPackageTypeName() + "");
        cardFavReq.setBindCardNo(dataBean.getBindCardNo());
        cardFavReq.setPackageType(dataBean.getPackageType() + "");
        cardFavReq.setSceneSpots(dataBean.getScenerySpots() + "");
        cardFavReq.setCustName(dataBean.getCustName());
        cardFavReq.setSpareTimes(dataBean.getSpareTimes());
        cardFavReq.setStateName(dataBean.getStateName());
        String json = new Gson().toJson(cardFavReq);
        L.e(Constant.TAG, "addUserCardFav===" + json);
        mNetworkDateSource.addUserCardFav(cardFavReq, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                ((IUserAddCardContact.IUserAddCardView) mView).addUserCardFavSucc(response);
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((IUserAddCardContact.IUserAddCardView) mView).addUserCardFavFail(response.getMessage());
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
                ((IUserAddCardContact.IUserAddCardView) mView).addUserCardFavFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void getUserBindCard() {
        AccessTokenRequest request = new AccessTokenRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        request.setAccess_token(access_token);
        mNetworkDateSource.getUserBindCard(request, new SubscribeResult<GetUserBindCardRes>() {
            @Override
            protected void onOk(GetUserBindCardRes response) {
//                List<UserCardFavResponse.DataBean> beanList = response.getData();
                if (mView == null) {
                    return;
                }
                ((IUserAddCardContact.IUserAddCardView) mView).getBindCardSucc(response.getMessage());
            }

            @Override
            protected void onNext(GetUserBindCardRes response) {
                super.onNext(response);
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
            }

            @Override
            protected void onFailure(GetUserBindCardRes response) {
                super.onFailure(response);
                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), throwable.getMessage());
            }
        });
    }
}
